﻿using System.ComponentModel;
using System.Runtime.InteropServices;

namespace TombMedia.Extensions.NETBIOS
{
    public class NetBiosUtility
    {
        [DllImport("netapi32.dll", CharSet = CharSet.Auto)]
        static extern int NetWkstaGetInfo(string server, int level, out WKSTA_INFO_100 info);

        [DllImport("netapi32.dll")]
        static extern int NetApiBufferFree(WKSTA_INFO_100 info);

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        class WKSTA_INFO_100
        {
            public int wki100_platform_id;
            public string wki100_computername;
            public string wki100_langroup;
            public int wki100_ver_major;
            public int wki100_ver_minor;
        }

        public static string GetMachineNetBiosDomain()
        {
            WKSTA_INFO_100 info;
            int retval = NetWkstaGetInfo(null, 100, out info);
            if (retval != 0)
                throw new Win32Exception(retval);
            string domainName = info.wki100_langroup;
            NetApiBufferFree(info);
            return domainName;
        }

        public static string GetMachineComputerName()
        {
            WKSTA_INFO_100 info;
            int retval = NetWkstaGetInfo(null, 100, out info);
            if (retval != 0)
                throw new Win32Exception(retval);
            string domainName = info.wki100_computername;
            NetApiBufferFree(info);
            return domainName;
        }
    }
}
